package pl.iachoo.ctl.tabory.ui.editors.vagons.composites;

import java.util.Date;

import org.apache.log4j.Logger;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import pl.iachoo.ctl.tabory.dao.data.vo.CompositionVO;
import pl.iachoo.ctl.tabory.dao.data.vo.VagonVO;
import pl.iachoo.ctl.tabory.ui.base.CommonValidationUtil;
import pl.iachoo.ctl.tabory.ui.base.masterdetail.AbstractDetailSectionPart;
import pl.iachoo.ctl.tabory.ui.editors.vagons.VagonSelection;
import pl.iachoo.ctl.tabory.ui.properties.Messages;
import pl.iachoo.ctl.tabory.ui.selection.ISelectionListener;
import pl.iachoo.ctl.tabory.ui.utils.CommonDateUtils;

public class VagonDetailSectionPart extends
		AbstractDetailSectionPart<VagonSelection> {

	private final static Logger logger = Logger
			.getLogger(VagonDetailSectionPart.class);

	private Composite client;

	private Text vagonName;
	private DateTime revisionOneFromControl;
	private DateTime revisionOneUntilControl;
	private DateTime revisionTwoFromControl;
	private DateTime revisionTwoUntilControl;
	private Combo compositionCombo;
	private Text revisionDistance;
	private Text revisionCorrection;
	private Text distance;
	private Button isActiveButton;

	public VagonDetailSectionPart(Composite parent, FormToolkit toolkit,
			int style, String sectionName) {
		super(parent, toolkit, style, sectionName);
	}

	@Override
	public VagonSelection getSelection() {
		logger.debug("Retriving selection.");
		try {
			VagonVO selectedVagon = selection.getSelectedVagon();
			if (selectedVagon == null) {
				selectedVagon = new VagonVO();
			}
			selectedVagon.setActive(isActiveButton.getSelection());
			if (selection.getListCompositions() != null) {
				if (compositionCombo.getSelectionIndex() != -1) {
					CompositionVO compositionVO = selection
							.getListCompositions().get(
									compositionCombo.getSelectionIndex());
					selectedVagon.setComposition(compositionVO);
				}
			}
			selectedVagon.setInternalId(vagonName.getText());
			selectedVagon.setRevisionCorrection(Double
					.parseDouble(revisionCorrection.getText()));
			selectedVagon.setRevisionDistance(Double
					.parseDouble(revisionDistance.getText()));
			Date dateRevOneFrom = CommonDateUtils.getDate(
					revisionOneFromControl.getYear(),
					revisionOneFromControl.getMonth(),
					revisionOneFromControl.getDay());
			Date dateRevOneUntil = CommonDateUtils.getDate(
					revisionOneUntilControl.getYear(),
					revisionOneUntilControl.getMonth(),
					revisionOneUntilControl.getDay());
			Date dateRevTwoFrom = CommonDateUtils.getDate(
					revisionTwoFromControl.getYear(),
					revisionTwoFromControl.getMonth(),
					revisionTwoFromControl.getDay());
			Date dateRevTwoUntil = CommonDateUtils.getDate(
					revisionTwoUntilControl.getYear(),
					revisionTwoUntilControl.getMonth(),
					revisionTwoUntilControl.getDay());
			selectedVagon.setRevOneFrom(dateRevOneFrom);
			selectedVagon.setRevOneUntil(dateRevOneUntil);
			selectedVagon.setRevTwoFrom(dateRevTwoFrom);
			selectedVagon.setRevTwoUntil(dateRevTwoUntil);
			selection.setSelectedVagon(selectedVagon);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return selection;
	}

	@Override
	public void setSelection(VagonSelection selection) {
		this.selection = selection;
		postInitGui(null, null);
		switch (selection.getEditorState()) {
		case NEW:
			vagonName.setText("...");
			distance.setText("");
			revisionCorrection.setText("...");
			revisionDistance.setText("...");
			vagonName.setText("...");
			vagonName.setText("...");
			isActiveButton.setSelection(false);
			client.setVisible(true);
			setEnabled(true);
			break;
		case CLEAN:
			fillEditorValues();
			setEnabled(false);
			break;
		case EDITING:
			fillEditorValues();
			setEnabled(true);
			break;
		}
	}

	private void fillEditorValues() {
		VagonVO selectedVagon = selection.getSelectedVagon();
		if (selectedVagon != null) {
			client.setVisible(true);
			vagonName.setText(selectedVagon.getInternalId());
			distance.setText(String.valueOf(selectedVagon.getDistance()));
			revisionCorrection.setText(String.valueOf(selectedVagon
					.getRevisionCorrection()));
			revisionDistance.setText(String.valueOf(selectedVagon
					.getRevisionDistance()));
			isActiveButton.setSelection(selectedVagon.isActive());
			Date dateRevOneFrom = selectedVagon.getRevOneFrom();
			Date dateRevOneUntil = selectedVagon.getRevOneUntil();
			Date dateRevTwoFrom = selectedVagon.getRevTwoFrom();
			Date dateRevTwoUntil = selectedVagon.getRevTwoUntil();
			revisionOneFromControl.setDate(
					CommonDateUtils.getYear(dateRevOneFrom),
					CommonDateUtils.getMonth(dateRevOneFrom),
					CommonDateUtils.getDay(dateRevOneFrom));
			revisionOneUntilControl.setDate(
					CommonDateUtils.getYear(dateRevOneUntil),
					CommonDateUtils.getMonth(dateRevOneUntil),
					CommonDateUtils.getDay(dateRevOneUntil));
			revisionTwoFromControl.setDate(
					CommonDateUtils.getYear(dateRevTwoFrom),
					CommonDateUtils.getMonth(dateRevTwoFrom),
					CommonDateUtils.getDay(dateRevTwoFrom));
			revisionTwoUntilControl.setDate(
					CommonDateUtils.getYear(dateRevTwoUntil),
					CommonDateUtils.getMonth(dateRevTwoUntil),
					CommonDateUtils.getDay(dateRevTwoUntil));
		} else {
			client.setVisible(false);
		}

	}

	@Override
	public void addSelectionListener(ISelectionListener<VagonSelection> listner) {

	}

	@Override
	public void removeSelectionListener(
			ISelectionListener<VagonSelection> listner) {

	}

	@Override
	protected void initGui(Section parentSection, FormToolkit toolkit) {
		logger.debug("Initializing GUI.");

		GridLayoutFactory.fillDefaults().numColumns(1).applyTo(parentSection);
		client = toolkit.createComposite(parentSection);
		GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3)
				.applyTo(client);
		parentSection.setClient(client);

		Label labelVagonName = toolkit.createLabel(client,
				Messages.getString("vagons.editor.label.vagon.vagonname"));
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(labelVagonName);
		vagonName = toolkit.createText(client, null);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).hint(120, SWT.DEFAULT).applyTo(vagonName);

		Label labelCompositionName = toolkit.createLabel(client, Messages
				.getString("vagons.editor.label.vagon.compositioninternalid"));
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(labelCompositionName);
		compositionCombo = new Combo(client, SWT.DROP_DOWN | SWT.READ_ONLY);
		toolkit.adapt(compositionCombo);
		toolkit.paintBordersFor(compositionCombo);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).hint(120, SWT.DEFAULT)
				.applyTo(compositionCombo);

		Label labelRevisionOneFrom = toolkit
				.createLabel(client, Messages
						.getString("vagons.editor.label.vagon.revisiononefrom"));
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(labelRevisionOneFrom);
		revisionOneFromControl = new DateTime(client, SWT.DATE | SWT.BORDER);
		toolkit.adapt(revisionOneFromControl);
		toolkit.paintBordersFor(revisionOneFromControl);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).hint(80, SWT.DEFAULT)
				.applyTo(revisionOneFromControl);

		Label labelRevisionOneUntil = toolkit.createLabel(client, Messages
				.getString("vagons.editor.label.vagon.revisiononeuntil"));
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(labelRevisionOneUntil);
		revisionOneUntilControl = new DateTime(client, SWT.DATE | SWT.BORDER);
		toolkit.adapt(revisionOneUntilControl);
		toolkit.paintBordersFor(revisionOneUntilControl);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).hint(80, SWT.DEFAULT)
				.applyTo(revisionOneUntilControl);

		Label labelRevisionTwoFrom = toolkit
				.createLabel(client, Messages
						.getString("vagons.editor.label.vagon.revisiontwofrom"));
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(labelRevisionTwoFrom);
		revisionTwoFromControl = new DateTime(client, SWT.DATE | SWT.BORDER);
		toolkit.adapt(revisionTwoFromControl);
		toolkit.paintBordersFor(revisionTwoFromControl);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).hint(80, SWT.DEFAULT)
				.applyTo(revisionTwoFromControl);

		Label labelRevisionTwoUntil = toolkit.createLabel(client, Messages
				.getString("vagons.editor.label.vagon.revisiontwountil"));
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(labelRevisionTwoUntil);
		revisionTwoUntilControl = new DateTime(client, SWT.DATE | SWT.BORDER);
		toolkit.adapt(revisionTwoUntilControl);
		toolkit.paintBordersFor(revisionTwoUntilControl);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).hint(80, SWT.DEFAULT)
				.applyTo(revisionTwoUntilControl);

		Label labelRevisionDistance = toolkit.createLabel(client, Messages
				.getString("vagons.editor.label.vagon.revisiondistance"));
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(labelRevisionDistance);
		revisionDistance = toolkit.createText(client, null);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).hint(120, SWT.DEFAULT)
				.applyTo(revisionDistance);

		Label labelRevisionCorrection = toolkit.createLabel(client, Messages
				.getString("vagons.editor.label.vagon.revisioncorrection"));
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(labelRevisionCorrection);
		revisionCorrection = toolkit.createText(client, null);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).hint(120, SWT.DEFAULT)
				.applyTo(revisionCorrection);

		Label labelDistance = toolkit.createLabel(client,
				Messages.getString("vagons.editor.label.vagon.distance"));
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(labelDistance);
		distance = toolkit.createText(client, null);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).hint(120, SWT.DEFAULT).applyTo(distance);

		isActiveButton = toolkit.createButton(client,
				Messages.getString("vagons.editor.label.vagon.isactive"),
				SWT.CHECK);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).hint(80, SWT.DEFAULT).span(1, 2)
				.applyTo(isActiveButton);

		toolkit.adapt(client);
		toolkit.paintBordersFor(client);
		client.setVisible(false);

	}

	@Override
	protected void postInitGui(Section parentSection, FormToolkit toolkit) {
		if (selection == null) {
			return;
		}
		compositionCombo.removeAll();
		for (CompositionVO composition : selection.getListCompositions()) {
			compositionCombo.add(composition.getInternalId());
		}
		if (selection != null) {
			VagonVO selectedVagon = selection.getSelectedVagon();
			if (selectedVagon != null) {
				CompositionVO composition = selectedVagon.getComposition();
				if (composition != null) {
					int index = -1;
					for (CompositionVO cmp : selection.getListCompositions()) {
						if (cmp.getId().equals(composition.getId())) {
							index = selection.getListCompositions()
									.indexOf(cmp);
							break;
						}
					}
					compositionCombo.select(index);
				}
			}
		}
		revisionCorrection.addKeyListener(CommonValidationUtil
				.getValidationDoubleKeyAdapter());
		revisionDistance.addKeyListener(CommonValidationUtil
				.getValidationDoubleKeyAdapter());
	}

	public void setEnabled(boolean isEnabled) {
		vagonName.setEnabled(isEnabled);
		revisionOneFromControl.setEnabled(isEnabled);
		revisionOneUntilControl.setEnabled(isEnabled);
		revisionTwoFromControl.setEnabled(isEnabled);
		revisionTwoUntilControl.setEnabled(isEnabled);
		revisionDistance.setEnabled(isEnabled);
		compositionCombo.setEnabled(isEnabled);
		revisionCorrection.setEnabled(isEnabled);
		distance.setEnabled(false);
		isActiveButton.setEnabled(isEnabled);
	}

	@Override
	protected void initActions() {

	}

}
